package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GlobalExceptionHandler;
import com.atguigu.servicebase.exceptionhandler.SelfExceptionHandler;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-05
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 添加课程
     * @param courseInfoVo
     * @return
     */
   @PostMapping("/addCourse")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo)
    {
        String courseId = courseService.addCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    /**
     * 点击上一步，实现信息回显
     * @param courseId  课程id
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String  courseId)
    {
       CourseInfoVo courseInfoVo=courseService.getCourseInfoByID(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    /**
     * 更新课程信息
     *
     * @return
     */
    @PutMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo )
    {
       courseService.updateCourseInfo(courseInfoVo);
       return R.ok();
    }

    /**
     * 根据课程id获取课程最终发布信息
     * @param courseId
     * @return
     */
    @GetMapping("/getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(@PathVariable String courseId)
    {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo );

    }

    /**
     * 最终发布课程
     * 将status字段改一个状态就行
     * @param courseId
     * @return
     */
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId)
    {
        EduCourse course=new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        //变了属性才会更新，原来的不会变
        courseService.updateById(course);
        return R.ok();
    }

    /**
     * 查询所有课程,列表显示，根据更新时间显示,降序
     * @return
     */
    @GetMapping("/list")
    public R getAllCourse()
    {
        List<EduCourse> courseList = courseService.getCourseList();
        return R.ok().data("courseList",courseList);
    }


    /**
     * 分页查询
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/pageList/{current}/{limit}")
   public R pageList(@PathVariable long current,@PathVariable long limit)
    {

        Map<String, Object> map = courseService.pageList(current, limit);
        return R.ok().data(map);
    }

    /**
     * 条件查询带分页，搜索框
     * @param current
     * @param limit
     * @param eduCourse
     * @return
     */
    @PostMapping("/pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current,@PathVariable long limit,@RequestBody(required = false) EduCourse eduCourse)
    {
        Map<String, Object> map = courseService.pageConditionList(current, limit, eduCourse);
        return R.ok().data(map);
    }

    /**
     * 删除课程对应的信息，小节，章节，视频等
     *
     * @param courseId
     * @return
     */
    @DeleteMapping("/removeCourse/{courseId}")
    public R removeCourse(@PathVariable String courseId)
    {
        courseService.removeCourse(courseId);
        return R.ok();
    }


}

